#include "forces.h"

ForceGenerator::~ForceGenerator(void)
{
	
}

/* CLASSE FORCE REGISTRY */

ForceRegistry::ForceRegistry(void)
{
	registry.reserve(50);
}

void ForceRegistry::add(WorldEntity *entity, ForceGenerator *gen)
{
	
}

void ForceRegistry::remove(WorldEntity *entity, ForceGenerator *gen)
{
	
}

void ForceRegistry::clear()
{
	registry.clear();
}

void ForceRegistry::updateForces(double delta)
{
	Registry::iterator i = registry.begin();
	for (; i != registry.end(); i++)
	{
		i->forceGen->updateForce(i->entity, delta);
	}
}

/* CLASSE GRAVITY GENERATOR */

GravityGenerator::GravityGenerator(const Vector2D &gravity)
{
	m_gravity.x = gravity.x;
	m_gravity.y = gravity.y;
}

GravityGenerator::~GravityGenerator(void)
{
	
}

void GravityGenerator::updateForce(WorldEntity *entity, float delta)
{
	if (!entity->hasFiniteMass())
		return;

	entity->addForce(-m_gravity);
}

/* CLASSE DRAG GENERATOR */

DragGenerator::DragGenerator(float k1, float k2)
{
	m_k1 = k1;
	m_k2 = k2;
}

DragGenerator::~DragGenerator(void)
{
	
}

void DragGenerator::updateForce(WorldEntity *entity, float delta)
{
	Vector2D force;
	
	/* recupera la velocita' */
	entity->getVelocity(&force);
	
	float c = force.magnitude();
	
	/* calcola il nuovo fattore */
	c = m_k1*c + m_k2*c*c;
	
	/* calcola la forza e la applica */
	force.normalize();
	force *= c;
	entity->addForce(force);
	
}
